package com.cens.mall.order.service.impl;

import com.cens.mall.order.entity.OrderEntity;
import com.cens.mall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cens.common.utils.PageUtils;
import com.cens.common.utils.Query;

import com.cens.mall.order.dao.OrderItemDao;
import com.cens.mall.order.entity.OrderItemEntity;
import com.cens.mall.order.service.OrderItemService;


@RabbitListener(queues = {"hello-java.queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 队列可以很多人都监听，但只能有一个人收到消息。只要有人收到消息，队列就会删除消息
     * 只有消息处理完成，方法运行结束，才可以接收到下一个消息
     * @param message 原生消息信息
     * @param content 消息体
     * @param channel 传输数据的通道
     */
    @RabbitHandler
    public void receive(Message message, OrderReturnReasonEntity content, Channel channel) {
        byte[] body = message.getBody();
        System.out.println(message);
        System.out.println(content);
        System.out.println(channel);
        try {
            //deliveryTag 是 channel 内自增的(从1开始)
            // 手动回复
            //同意
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            //拒绝
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        } catch (IOException e) {
            System.out.println(e);
        }
    }
    @RabbitHandler
    public void receive2(Message message, OrderEntity content, Channel channel) {
        byte[] body = message.getBody();
        System.out.println(message);
        System.out.println(content);
        System.out.println(channel);
    }
}